package com.leha.mp.core.services.imp;

import com.leha.mp.core.dao.PlBedgeMapper;
import com.leha.mp.core.dao.PlBedgeTypeMapper;
import com.leha.mp.core.entity.mybatis.PlBedge;
import com.leha.mp.core.entity.mybatis.PlBedgeExample;
import com.leha.mp.core.entity.mybatis.PlBedgeKey;
import com.leha.mp.core.entity.mybatis.PlBedgeType;
import com.leha.mp.core.services.BedgeServices;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/***

 */


@Service
@Log4j2
public class BedgeServicesImpl implements BedgeServices {
    @Autowired
    PlBedgeMapper bedgeMapper;
    @Autowired
    PlBedgeTypeMapper bedgeTypeMapper;

    @Override
    public Integer UpdateUserBedge(Integer uid, Integer type, Integer refid, Integer num) {
        try {
            PlBedgeKey key = new PlBedgeKey();
            key.setUserId(uid);
            key.setType(type);
            key.setRefId(refid);
            PlBedge bedge = bedgeMapper.selectByPrimaryKey(key);
            if(bedge!=null) {
                bedge.setCnt(bedge.getCnt() + num);
                bedgeMapper.updateByPrimaryKey(bedge);
                upperLevelBedgeUpdate(uid, type);
                return bedge.getCnt();
            } else {
                PlBedge b = new PlBedge();
                b.setCnt(num);
                b.setType(type);
                b.setRefId(refid);
                b.setUserId(uid);
                bedgeMapper.insertSelective(b);
                upperLevelBedgeUpdate(uid, type);
                return num;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Integer UpdateDoctorBedge(Integer uid, Integer type, Integer refid, Integer num) {
        return UpdateUserBedge(-uid, type, refid, num);
    }

    @Override
    public HashMap<String, Integer> ClearUserBedge(Integer uid, Integer type) {
        try {
            PlBedgeExample ex = new PlBedgeExample();
            ex.createCriteria().andUserIdEqualTo(uid).andTypeEqualTo(type);
            bedgeMapper.deleteByExample(ex);
            upperLevelBedgeUpdate(uid, type);
        } catch (Exception e) {
            log.info("用户："+uid+"没有对应红点");
        }
        return getUserBedge(uid);
    }

    @Override
    public HashMap<String, Integer> ClearUserBedge(Integer uid, Integer type, Integer refid) {
        try {
            PlBedgeExample ex = new PlBedgeExample();
            ex.createCriteria().andUserIdEqualTo(uid).andTypeEqualTo(type).andRefIdEqualTo(refid);
            bedgeMapper.deleteByExample(ex);
            upperLevelBedgeUpdate(uid, type);
        } catch (Exception e) {
            log.info("用户："+uid+"没有对应红点");
        }
        return getUserBedge(uid);
    }

    private Integer SetUserBedge(Integer uid, Integer type, Integer refid, Integer num) {
        try {
            PlBedgeKey key = new PlBedgeKey();
            key.setUserId(uid);
            key.setType(type);
            key.setRefId(refid);
            PlBedge bedge = bedgeMapper.selectByPrimaryKey(key);
            if(bedge!=null) {
                bedge.setCnt(num);
                bedgeMapper.updateByPrimaryKey(bedge);
                return bedge.getCnt();
            } else {
                PlBedge b = new PlBedge();
                b.setCnt(num);
                b.setType(type);
                b.setRefId(refid);
                b.setUserId(uid);
                bedgeMapper.insertSelective(b);
                return num;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    private void upperLevelBedgeUpdate(Integer uid, Integer typeid) {
        PlBedgeType type = bedgeTypeMapper.selectByPrimaryKey(typeid);
        if (type == null || type.getParrentId()==0) {
            return;
        }
        PlBedgeExample ex = new PlBedgeExample();
        ex.createCriteria().andUserIdEqualTo(uid).andTypeEqualTo(typeid).andCntGreaterThan(0);
        List<PlBedge> res = bedgeMapper.selectByExample(ex);
        Integer sum = 0;
        for(PlBedge red: res) {
            sum += red.getCnt();
        }
        SetUserBedge(uid, type.getParrentId(), typeid, sum);
    }

    @Override
    public HashMap<String, Integer> getUserBedge(Integer uid) {
        PlBedgeExample ex = new PlBedgeExample();
        ex.createCriteria().andCntGreaterThan(0).andUserIdEqualTo(uid);
        List<PlBedge> bedges = bedgeMapper.selectByExample(ex);
        HashMap<String, Integer> va = new HashMap<>();
        for(PlBedge be: bedges) {
            Integer typeCnt = va.get(be.getType().toString());
            if(typeCnt != null) {
                va.put(be.getType().toString(), typeCnt+be.getCnt());
            }
            va.put(be.getType().toString()+"#"+be.getRefId().toString(), be.getCnt());
        }
        return va;
    }
}
